package com.scqkxx.gamebox.activity.group.adapter;

import android.app.Activity;
import android.content.ContentValues;
import android.widget.ImageView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.SaveCallback;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.game.adapter.GameGridAdapter;
import com.scqkxx.gamebox.activity.group.GroupMembersActivity;
import com.scqkxx.gamebox.activity.group.GroupTransferActivity;
import com.scqkxx.gamebox.widget.MySelfCallDialog;
import com.scqkzqtz.base.library.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by admin on 2018/8/15. 09点37分
 * name: 唐师
 * 用途 ： 游戏页面的适配器
 */

public class GroupMemberAdapter extends BaseQuickAdapter<AVObject, BaseViewHolder> {

    private MySelfCallDialog dialog;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");

    public GroupMemberAdapter(int layoutResId, List<AVObject> data) {
        super(layoutResId, data);
    }

    private int belongUserId = 0;

    public int getBelongUserId() {
        return belongUserId;
    }

    public void setBelongUserId(int belongUserId) {
        this.belongUserId = belongUserId;
    }

    @Override
    protected void convert(BaseViewHolder helper, AVObject item) {
        helper.setText(R.id.tv_group_name, item.getString("name"));

        if(item.getInt("userId") == belongUserId){//所有者
            helper.setText(R.id.tv_group_content, "所有者");
            helper.setText(R.id.bt_zr, "转让");
        }else {
            helper.setText(R.id.tv_group_content, "成员 "+simpleDateFormat.format(item.getCreatedAt()) + " 加入");
            helper.setText(R.id.bt_zr, "删除");
        }
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.icon_user_header_default)
                .error(R.mipmap.icon_user_header_default)
                .centerCrop()
                .circleCrop();
        Glide.with(mContext)
                .load(item.getString("avatar"))
                .apply(options)
                .into((ImageView) helper.convertView.findViewById(R.id.iv_group_img));
        helper.convertView.findViewById(R.id.bt_zr).setOnClickListener(view -> {
            if(item.getInt("userId") == belongUserId){//所有者
                ContentValues contentValues = new ContentValues();
                contentValues.put("groupId",item.get("groupId")+"");
                ActivityUtil.switchTo(mContext,GroupTransferActivity.class,contentValues);
            }else {
                if(dialog == null){
                    dialog = new MySelfCallDialog(mContext);
                }
                dialog.show("确定将成员"+item.getString("name")+"移除?", new MySelfCallDialog.OnConfirmListener() {
                    @Override
                    public void onSure() {
                        item.put("isDisable",1);
                        item.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(AVException e) {
                                if(e == null){
                                    ToastUtils.showShortToast(mContext,"移除成功");
                                }
                            }
                        });
                    }
                });
            }
        });

    }
}
